package soul.web.resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import soul.utils.DateUtils;
import soul.web.vo.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/3/11.
 */
@Controller
@RequestMapping("/task")
public class STaskResource {
    @Autowired
    private TaskService taskService;
    @Autowired
    private IdentityService identityService;

    /**
     * 所有激活的任务
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Response list() throws Exception {
        List<Task> tasks = taskService.createTaskQuery().active().list();
        List<Map<String, String>> list = new ArrayList<>();
        tasks.forEach(task -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("taskName", task.getName());
            map.put("id", task.getId());
            map.put("time", DateUtils.formatDate(task.getCreateTime(), DateUtils.PATTERN_ONE));
            map.put("assignee", task.getAssignee());
            list.add(map);
        });
        return Response.ok(list);
    }

    @RequestMapping(value = "/do_task/{task_id}")
    @ResponseBody
    public Response doTask(@PathVariable("task_id") String taskId) {
        taskService.complete(taskId);
        return Response.ok();
    }

    /**
     * 待办任务--Portlet
     */
    @RequestMapping(value = "/todo_list")
    @ResponseBody
    public Response todoList() throws Exception {

        // 已经签收的任务
        List<Task> todoList = taskService.createTaskQuery().taskAssignee("admin").active().list();


        // 等待签收的任务
        List<Task> toClaimList = taskService.createTaskQuery().taskCandidateUser("admin").active().list();


        return Response.ok();
    }

}
